﻿using System;
using System.Text;
using System.Collections.Generic;


namespace EpisodeWizard.Core
{

	/// <summary>
	/// Caches fuzzy string results
	/// </summary>
	static class FuzzyStringCache
	{
		/// <summary>
		/// Token to separate the two strings, forming a key for the dictionary
		/// </summary>
		private static string Separator = "|||";


		/// <summary>
		/// Dictionary of cached distances
		/// </summary>
		private static Dictionary<string, float> CachedDistances = new Dictionary<string, float>();


		/// <summary>
		/// Gets the distance between 2 strings
		/// Adds the result to cache to avoid computing it the next time
		/// </summary>
		public static float GetCachedDistance(string Str1, string Str2, int MaxOffset)
		{
			string Key = Str1 + Separator + Str2;
			float Distance;

			if (CachedDistances.TryGetValue(Key, out Distance))
			{
				return Distance;
			}
			else
			{
				Distance = FuzzyString.DistanceSift3(Str1, Str2, MaxOffset);
				CachedDistances.Add(Key, Distance);
				return Distance;
			}
		}

	}

}
